package com.xdwanj.dao.impl;

import com.xdwanj.dao.AdminUserDao;
import com.xdwanj.dao.BaseDao;
import com.xdwanj.pojo.AdminUser;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class AdminUserDaoImpl implements AdminUserDao {

    BaseDao baseDao = new BaseDao();

    JdbcTemplate jdbcTemplate = baseDao.getJdbcTemplate();

    @Override
    public AdminUser findUserByUsernameAndPassword(String username, String password) throws Exception {
        String sql = "select * from admin_user ad where ad.username = ? and ad.password = ?";

        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<AdminUser>(AdminUser.class), username, password);
    }

    @Override
    public int deleteByEmpId(int eid) throws Exception {
//        String sql = "delete admin_user\n" +
//                "from admin_user\n" +
//                "where id in (\n" +
//                "    select eai.id\n" +
//                "    from (\n" +
//                "             select au.id id\n" +
//                "             from emp e,\n" +
//                "                  admin_user au\n" +
//                "             where e.id = au.emp_id\n" +
//                "               and e.id = ?\n" +
//                "         ) eai\n" +
//                ")";


        String sql = "delete admin_user from admin_user where emp_id = ?;";

        return jdbcTemplate.update(sql, eid);
    }
}
